package cn.handong.tool.job_find;

import org.junit.Test;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

import java.io.*;
import java.lang.annotation.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class 读写txt {
    /**
     * 逐行读取TXT
     * @throws IOException
     */
    @Test
    public void mkMyMd() throws IOException {
        List<String> fileNames = new ArrayList<>();
        fileNames.add("Java.md");
        fileNames.add("Linux和操作系统.md");
        fileNames.add("公司相关.md");
        fileNames.add("场景题.md");
        fileNames.add("Spring.md");
        fileNames.add("微服务与分布式.md");
        fileNames.add("数据库.md");
        fileNames.add("前端.md");
        readTxtAndWriteTxt(fileNames);
    }

    public void readTxtAndWriteTxt(List<String> fileNames) throws IOException {
        //绝对路径
        String basePath = "D:\\Idea_WorkSpace\\my-study-notes\\跳槽准备\\面试回答话术\\";
        List<String> messageList = new ArrayList<>();
        for (String fileName : fileNames) {
            messageList.add("************************");
            messageList.add("# " + fileName.split("\\.")[0]);
            messageList.add("************************");
            String path = basePath + fileName;
            BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(path)), StandardCharsets.UTF_8));
            String lineTxt;
            Integer cur = 0;
            Integer prex = 0;
            // 逐行读取
            while ((lineTxt = br.readLine()) != null) {
                if (lineTxt.length() > 0 && lineTxt.charAt(0) == '>'){
                    if (cur != prex + 1){
                        messageList.add("");
                    }
                    messageList.add(lineTxt);
                    prex = cur; // 指向上一页
                }
                cur++; // 当前页后移
            }
            br.close();
        }
        String folder = "D:\\User\\Desktop";
        String file = "大笔记.md";
        writeToTxt(folder, file, messageList);
    }

    /**
     * 写入txt
     * @throws FileNotFoundException
     */
    @Test
    public void readExcelAndWriteTxt() throws FileNotFoundException {
        //写入txt
        String folder = "C:\\Users\\sqkb8v\\IdeaProjects\\my-general-tool";
        String file = "test.txt";
        List<String> messageList = new ArrayList<>();
        messageList.add("流浪地球");
        messageList.add("战狼");
        messageList.add("吴京");
        messageList.add("沈腾");
        messageList.add("林俊杰");
        messageList.add("五月天");
        writeToTxt(folder, file, messageList);
    }

    public void writeToTxt(String folder, String file, List<String> messageList) throws FileNotFoundException {
        File files = new File(folder);
        files.mkdirs();

        FileOutputStream fos = new FileOutputStream(folder + File.separator + file);

        // 逐行写入
        PrintWriter pw = new PrintWriter(fos);
        for (String message : messageList) {
            pw.println(message);
        }
        pw.close();
    }
}


